package com.mystore.util;



import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBUtil {

    private static final String driver = "com.mysql.jdbc.Driver";
    private static final String url = "jdbc:mysql://localhost:3306/shop";
    private static final String username = "root";
    private static final String password = "787800";


    /**
     * 连接数据库连接对象
     * @param driverClass
     * @param dbUrl
     * @param dbUsername
     * @param dbPassword
     * @return
     */
    public static Connection getConntion(String driverClass,String dbUrl,String dbUsername,String dbPassword){

        try {
            Class.forName(driverClass);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try {
            return DriverManager.getConnection(dbUrl,dbUsername,dbPassword);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Connection getConntion(){
        return getConntion(driver,url,username,password);
    }

    public static void closeConnection(Connection connection){
        try {
            if(!connection.isClosed()) {
                if (connection != null) {
                        connection.close();
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


}
